<?php

/**
 * AuthController
 *
 * @author Maodo DIOP <dmaodo@gmail.com>
 * @version
 */

require_once 'Zend/Controller/Action.php';
require_once 'Zend/Auth.php';
require_once 'Zend/Auth/Adapter/DbTable.php';

class Admin_PointbrController extends Zend_Controller_Action {

	public function indexAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
		$pointbr = new Admin_Model_DbTable_Pointbr();
		$res = $pointbr->getAllPointbr();
		$resultat = Zend_Json :: encode($res);
		//$this->_response->setHeader('Content-Type', 'text/x-json');
		$this->_response->setBody($resultat);
	}

	public function addAction() {
		$this->view->title = "Ajouter Point GMV";
		$this->view->headTitle($this->view->title);
		$form = new Admin_Form_Pointbr();
		$form->submit->setLabel('Ajouter');
		$this->view->form = $form;
		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
				$latitude = $form->getValue('latitude');
				$longitude = $form->getValue('longitude');
				$pointbrprec = $form->getValue('pointbrprec');
				$pointbr = new Admin_Model_DbTable_Pointbr();
				$pointbr->addPointbr($latitude,$longitude,$pointbrprec);
				$this->_helper->redirector('index');
			} else {
				$form->populate($formData);
			}
		}
	}

	public function editAction() {
		$form = new Admin_Form_Pointbr() ;
		$form->submit->setLabel('Enregistrer');
		$this->view->form = $form;
		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
				$idpointbr = (int)$this->_getParam('idpointbr');;
				$latitude = $form->getValue('latitude');
				$longitude = $form->getValue('longitude');
				$pointbrprec = $form->getValue('pointbrprec');
				$pointbr = new Admin_Model_DbTable_Pointbr();
				$pointbr->updatePointbr($idpointbr, $latitude, $longitude, $pointbrprec);
				$res = array();
				$res = $pointbr->getPointbr($idpointbr);
				$this->_redirect('/admin/pointbr/index');
			} else {
				$form->populate($formData);
			}
		} else {
			//Si le formulaire n'est pas encore post�
			//Recuperer la valeur de l'objet � editer
			$idpointbr = $this->_request->getParam('idpointbr', 0);
			if ($idpointbr > 0) {
				$pointbr = new Admin_Model_DbTable_Pointbr();
				$form->populate($pointbr->getPointbr($idpointbr));
			}
		}
	}


	public function deleteAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
		$idpointbr = $this->_getParam('idpointbr', 0);
		$pointbr = new Admin_Model_DbTable_Pointbr();
		$pointbr->deletePointbr($idpointbr);
		$this->_redirect('/pointbr/index');
	}

	public function allpointbrAction() {
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->getHelper('layout')->disableLayout();
		$pointbr = new Admin_Model_DbTable_Pointbr();
		$res = $pointbr->getAllPointbr();
		$resultat = Zend_Json :: encode($res);
		$this->_response->setHeader('Content-Type', 'text/x-json');
		$this->_response->setBody($resultat);
	}
}
